package com.jinqing.wuchuang.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jinqing.wuchuang.entity.SettleCompany;
import com.jinqing.wuchuang.entity.SettledLegalRepresentative;
import com.jinqing.wuchuang.entity.SettledTeamMenber;
import com.jinqing.wuchuang.mapper.SettleCompanyMapper;
import com.jinqing.wuchuang.mapper.SettledTeamMenberMapper;
import com.jinqing.wuchuang.service.AuthUserService;
import com.jinqing.wuchuang.service.SettleCompanyService;
import com.jinqing.wuchuang.service.SettledLegalRepresentativeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SettleCompanyServiceImpl implements SettleCompanyService {
    @Autowired
    private SettleCompanyMapper settleCompanyMapper;
    @Autowired
    private AuthUserService authService;
    @Autowired
    private SettledTeamMenberMapper settledTeamMenberMapper;


    @Override
    public void insertCompany(SettleCompany settleCompany) {
        //不用返回，有异常往上抛就好，这里先不抛出异常，跟新时再做规范
        settleCompanyMapper.insert(settleCompany);
    }

    /**
     * 更新公司信息
     * @param settleCompany
     */
    @Override
    public void updateCompany(SettleCompany settleCompany) {
        //https://blog.csdn.net/qq_39715000/article/details/120090033
        UpdateWrapper<SettleCompany> settleCompanyUpdateWrapper= new UpdateWrapper<>();
        settleCompanyUpdateWrapper.eq("id",settleCompany.getId());
        settleCompanyMapper.update(settleCompany,settleCompanyUpdateWrapper);
    }


    /**
     * 获得当前法人的所有公司
     * @return
     */
    @Override
    public List<SettleCompany> getAllCompany() {
        //构造queryWrapper 查询所有就好
        Long uid = authService.getUidByToken();
        QueryWrapper<SettleCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        return settleCompanyMapper.selectList(queryWrapper);
    }
    /**
     * 根据公司的id获取详情信息，很简单
     * @param id
     * @return
     */
    @Override
    public SettleCompany getCompanyById(Integer id) {
        QueryWrapper<SettleCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return settleCompanyMapper.selectOne(queryWrapper);
    }


    /**
     * 根据公司id更新队员信息
     */
    @Override
    @Transactional
    public void updateTeammenber(List<SettledTeamMenber> settledTeamMenbers) {
        //使用名字作为唯一标识,既然能到这个service ，说明所有的队员的companyid是一样的
        //把公司的所有队员删掉，再把给的写入就好
        int companyId = settledTeamMenbers.get(0).getCompanyId();
        ArrayList<SettledTeamMenber> settledTeamMenber = (ArrayList<SettledTeamMenber>) getAllSettledTeamMenbers(companyId);
        QueryWrapper queryWrapper = new QueryWrapper<SettledTeamMenber>();
        queryWrapper.eq("company_id",companyId);
        settledTeamMenberMapper.delete(queryWrapper);
        for(SettledTeamMenber s:settledTeamMenbers){
            settledTeamMenberMapper.insert(s);
        }
    }

    @Override
    public List<SettledTeamMenber> getAllSettledTeamMenbers(int companyId) {
        QueryWrapper<SettledTeamMenber> queryWrapper = new QueryWrapper();
        queryWrapper.eq("company_id",companyId);
        return settledTeamMenberMapper.selectList(queryWrapper);
    }


}
